import base64
import json
from zeep import Client
from .des_encode import encode_des  # 使用相对导入
client = None
account = "Q004821"
secret = "Zjsw2020"
key = encode_des(account, secret)

def get_wsdl_client():
    global client
    if client is None:
        client = Client('http://wzjcjg.jzyglxt.com/service/jczx.asmx?wsdl')
    return client

def get_gcs():
    client = get_wsdl_client()
    req_json = json.dumps({
        "gcmc": "温州",
    })
    
    response = client.service.GetGcs(account, req_json, "10", "1", key)
    result = json.loads(response)
    return result

def get_single_entrust_bill(entrust_number: str):
    client = get_wsdl_client()
    response = client.service.GetWtd(account, entrust_number, key)
    result = json.loads(response)
    return result

def get_single_project_bill(project_number: str):
    client = get_wsdl_client()
    req_json = json.dumps({
        "gcbh": project_number,
    })
    response = client.service.GetGcs(account, req_json, "10", "1", key)
    result = json.loads(response)
    return result

def upload_report_data_with_file(entrust_bill: dict, pdf_file: bytes):
    client = get_wsdl_client()

    # PDF 文件转换为 base64 编码
    pdf_base64 = base64.b64encode(pdf_file).decode('utf-8')
    pdfjson = json.dumps([{"bgwj": pdf_base64}])
    
    # 准备参数
    dwbh = account
    wtdwyh = entrust_bill["entrust_number"]
    bgbh = entrust_bill["report_number"]
    syr = entrust_bill["experimenter"]
    shr = entrust_bill["reviewer"]
    qfr = entrust_bill["issuer"]
    syrq = entrust_bill["experiment_date"]
    qfrq = entrust_bill["issue_date"]
    jcjg = entrust_bill["result"]
    jcjgms = entrust_bill["result_description"]
    mdatajson = json.dumps(entrust_bill["main_data"])
    sdatajson = json.dumps(entrust_bill["sub_data"])

    # 调用接口
    response = client.service.UpReport2(
        dwbh,
        wtdwyh,
        bgbh,
        syr,
        shr,
        qfr,
        syrq,
        qfrq,
        jcjg,
        jcjgms,
        mdatajson,
        sdatajson,
        pdfjson,
        "",
        key
    )
    
    return json.loads(response)

def get_report_qr_file(entrust_number: str):
    client = get_wsdl_client()
    response = client.service.GetReport(account, entrust_number, key)
    result = json.loads(response)
    return result

#设置送样单位1
def set_WtdSydw(entrust_number_list:list):
    client = get_wsdl_client()
    id_list = []
    
    for entrust_number in entrust_number_list:
        id_list.append({"wtdwyh": entrust_number,
                        })
    
    response = client.service.SetWtdSydw(account,json.dumps(id_list),key)
    result = json.loads(response)
    return result
#设置送样单位2
def set_WtdSydw2(data_list:list):
    client = get_wsdl_client()
    response = client.service.SetWtdSydw2(account,json.dumps(data_list),key)
    result = json.loads(response)
    return result
#设置委托单编号
def set_SLWtdbh(data:list):
    client = get_wsdl_client()
    response = client.service.SetSlWtdbh(account,json.dumps(data),key)
    result = json.loads(response)
    return result
#上传原始数据和图片
def up_data(data:dict[str,any]):
    client = get_wsdl_client()
    response = client.service.UpData(account,
                          data.get("wtdwyh"),
                          data.get("syxmmc"),
                          data.get("ypbh"),
                          data.get("zh"),
                          data.get("syr"),
                          data.get("sysb"),
                          data.get("sykssj"),
                          data.get("syjssj"),
                          data.get("syqx"),
                          data.get("Videofiles",""),
                          data.get("recordfiles",""),
                          data.get("datajson"),
                          data.get("czdatajson"),
                          data.get("sfbc"),
                          key                          
    )
    result = json.loads(response)
    return result